<div>
<?php 
echo $this->MForm->create($current_model);
?>
<style>
th {
    text-align: center;
}
</style>
  	<div >
  		<?PHP echo $this->MForm->hidden('id'); ?>
  		<?PHP echo $this->MForm->input('questiontype',array('default'=>'radio','style'=>'width:auto;')); ?>
  		<?PHP echo $this->MForm->input('name',array('rows'=>3)); ?>
  	</div>
    <div>
    	<input type="hidden" name="questionnaire_id" value="{{$_REQUEST['questionnaire_id']}}">
          <?PHP echo $this->MForm->input('is_require',array('default'=>1)); ?>
  	</div>
  	<?PHP echo $this->MForm->input('placeholder'); ?>
      	<!-- <section id="input-panel-set" style="display:none;">      		
      		<?PHP echo $this->MForm->input('format'); ?>
      	</section> -->
      	
      <div id="select-options-manage" >
      	<?PHP echo $this->MForm->hidden('minselect',array('value'=>1)); ?>
      	<!-- 
  		<section id="select-panel-set-check" style="display:none;">
          	<?PHP echo $this->MForm->input('minselect',array('value'=>1)); ?>
          	<?PHP echo $this->MForm->input('maxselect',array('value'=>0,'after'=>'0表示不限制多选数目')); ?>
        </section> -->
        
      		
      			<table align="center" width="100%">
      			<thead>
      			<tr  align="center"><th>选项</th><th>选项内容</th>{{if $_REQUEST['exam']}}<th>正确答案</th>{{/if}}{{if $_REQUEST['multi']}}<th>轴线</th>{{/if}}<th>操作</th></tr>
      			</thead>
      			<tbody  id="select-option-list" class="select-option-list">
      			<?PHP foreach($options_list as $key => $options) { 
      				$options = $options['Surveyoption'];
      			?>
					<tr align="center">
					<td class="optionName"><?PHP echo chr(65+$key); ?></td>
					<td><?PHP echo $this->MForm->hidden('Surveyoption.'.$key.'.id',array('value'=> $options['id']));
echo $this->MForm->hidden('Surveyoption.'.$key.'.qid',array('value'=> $options['qid']));
 echo $this->MForm->input('Surveyoption.'.$key.'.name',array('value'=>$options['name'],'placeholder'=>"[选项内容]",'required'=>'required','div'=>false,'label'=>false)); ?>
 					</td>
					
					{{if $_REQUEST['exam']}}
 						<td><?PHP echo $this->MForm->input('Surveyoption.'.$key.'.correct',array('type'=>'checkbox','value'=>1,'checked'=> $options['correct'],'label'=>false,'div'=>false)); ?></td>
					{{/if}}
				{{if $_REQUEST['multi']}}
					<td><?PHP echo $this->MForm->input('Surveyoption.'.$key.'.optiontype',array('type'=>'select','value' => $options['optiontype'],'options'=>array('x'=>'x','y'=>'y'),'div'=>false,'label'=>false)); ?></td>
				{{else}}
					<?PHP echo $this->MForm->hidden('Surveyoption.'.$key.'.optiontype',array('type'=>'select','value' => 'x','div'=>false,'label'=>false)); ?>
				{{/if}}
				<td width="62">
				<button class="btn deleteoption  btn-default" data-qid="{{$options['qid']}}" data-id="{{$options['id']}}" type="button">删除</button>
				</td></tr>
				<?PHP } ?>
				</tbody></table>
<button class="ui-state-default ui-corner-all" type="button" style="margin:5px 0;" id="addoptionlist">增加选项</button>
<p><br></p>
		<?PHP echo $this->MForm->input('columns',array('default'=>1,'style'=>'width:auto;')); ?>
	</div>
	{{if $_REQUEST['is_exam']}}
	<?PHP echo $this->MForm->input('content',array('rows'=>3)); ?>
	{{/if}}
	
  <div class="clear"></div>
  	<div class="form-group">
		<label class="col-sm-2 control-label"></label>
		<div class="col-sm-9"><input class="btn btn-primary" type="submit" value="提交"></div>
	</div>
{{$this->Form->end()}}

</div>

<script type="text/javascript" src="/js/jquery/jquery.validate.js"></script>
<script>


function resetOptionName(){
	var $chars = 'ABCDEFGHIJKLMNOPQRSTUVWXYZ';
	$('.optionName').each(function(i){
		$(this).html($chars.charAt(i));
	});
}

$(function(){
	$('#addoptionlist').click(function(){
		var i = $('#select-option-list tr').size();
		var str='<tr align="center"><td class="optionName"></td>'+
		'<td><input type="text" id="SurveyoptionSelectiName" class="form-control" placeholder="[选项内容]" name="data[Surveyoption][Selecti][name]"></td>'+
		
		{{if $_REQUEST['exam']}}'<td><input type="checkbox" id="SurveyoptionSelectiCorrect" value="1" name="data[Surveyoption][Selecti][correct]"></td>'+ {{/if}}
		
		{{if $_REQUEST['multi']}}
			'<td><?PHP echo $this->MForm->input('Surveyoption.'.$key.'.optiontype',array('type'=>'select','value' => $options['optiontype'],'options'=>array('x'=>'x','y'=>'y'),'div'=>false,'label'=>false)); ?></td>' +
		{{/if}}
		'<td width="62">{{if !$_REQUEST['multi']}}<input type="hidden" value="x" name="data[Surveyoption][Selecti][optiontype]">{{/if}}'+
		'<button class="btn deleteoption btn-default" type="button">删除</button></td>'+
		'</tr>';	
		i++;	
		str = str.replace(/Selecti/g,i);
		$('#select-option-list').append($(str));
		resetOptionName();
	});
	$('.deleteoption').on('click',function(){
		var obj = this;
		var id = $(this).data('id'),qid = $(this).data('qid');;
		if(id > 0){
			if(confirm('确定要从服务器删除这个选项？')){
				$.ajax({
					url:' {{Router::url('/surveys/delete_option/');}}'+qid+'/'+id,
					data:{},
					success:function(result,textStatus){
						if(result.success && result.success!=""){
							var thisli = $(obj).parents('tr')[0];
							$(thisli).remove();
							resetOptionName();
						}
						else{
							alert('删除失败');	
						}
					},
					dataType:"json",
				    cache:false
				});
			}
		}
		else{
			var thisli = $(obj).parents('tr')[0];
			$(thisli).remove();
			resetOptionName();
		}
		
	});
	
	$('#SurveyQuestiontype').bind('change',function(){
		var value = this.value;
		if(value=='checkbox' || value=='radio' || value=='select'){
			$('#select-options-manage').show();
		}
		else{
			$('#select-options-manage').hide();
			
		}
		
		if(value == 'input') {
			$('#input-panel-set').show();
		}
		else{
			$('#input-panel-set').hide();
		}
		
		
		if(value=='checkbox'){
			$('#select-panel-set-check').show();
		}
		else{
			$('#select-panel-set-check').hide();
		}
	}).trigger('change');
});
</script>